package com.h3c.nem.task;

import cn.hutool.core.thread.ThreadUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eos.common.util.IdWorker;
import com.h3c.nem.config.properties.EmailSendProperties;
import com.h3c.nem.entity.SdNoticeRecord;
import com.h3c.nem.entity.TbOfferWorker;
import com.h3c.nem.enums.OfferWorkerStatusEnum;
import com.h3c.nem.service.CommService;
import com.h3c.nem.service.ITbOfferWorkerService;
import com.h3c.nem.utils.BusiEnumDefine;
import com.h3c.nem.utils.EmailUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @author chenyifan
 * @date 2022/6/22 15:53
 */
@Slf4j
@Component
@AllArgsConstructor
@JobHandler("SendEmailNoFixup")
public class SendEmailForTbOfferWorker extends IJobHandler {


    private final ITbOfferWorkerService offerWorkerService;

    private final EmailSendProperties emailSendProperties;

    private final CommService commService;

    /**
     * 对未完成入职信息填报的员工，每天定时催促预备员工登记入职信息
     *
     * @param param
     * @return
     * @throws Exception
     */
    @Override
    public ReturnT<String> execute(String param) throws Exception {
        XxlJobLogger.log("开始执行SendEmailNoFixup");
        try {
            List<TbOfferWorker> noFixipMan = offerWorkerService.list(new QueryWrapper<TbOfferWorker>().lambda()
                    .eq(TbOfferWorker::getWorkerStatus, OfferWorkerStatusEnum.待填写入职资料)
            );
            for (TbOfferWorker offerWorker : noFixipMan) {
                ThreadUtil.execute(() -> EmailUtil.sendPtEmail(emailSendProperties.getNoFinishRemindTemplateCode(), null,
                        EmailUtil.getReceiveEmail(offerWorker.getWorkerId(), false, false, false, true),
                        "请完善入职信息", null, null));
                //发送短信给预备员工
                String emailContent = EmailUtil.getEmailTemplateContent(emailSendProperties.getNoFinishRemindTemplateCode(), null);
                SdNoticeRecord emailRecord = new SdNoticeRecord();
                emailRecord.setApplyUserId(commService.getLoginUserIdWithNoException());
                emailRecord.setNoticeType(BusiEnumDefine.NOTICE_TYPE_SMS);
                emailRecord.setTargetPhone(offerWorker.getPhoneNo());
                emailRecord.setNoticeTitle("请完善入职信息");
                emailRecord.setNoticeContent(emailContent);
                emailRecord.setCreateTime(new Date());
                emailRecord.setNoticeId(IdWorker.nextId() + "");
                commService.sendMessage(emailRecord);
            }
        } catch (Exception e) {
            log.error("定时任务SendEmailNoFixup失败,原因:{}", e.getMessage());
            return ReturnT.FAIL;
        }
        XxlJobLogger.log("结束SendEmailNoFixup");
        return ReturnT.SUCCESS;
    }


}
